//
//  MBForumDetailCell.m
//  MiBaoTu
//
//  Created by admin on 16/10/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBForumDetailCell.h"

@interface MBForumDetailCell () {
    UIView                  *_bgView;
    
    UILabel                 *_nickNameLb;
    
    UIButton                *_zhanKaiBt;
    UIButton                *_huiFuBt;
    
    UILabel                 *_huiNameLb;
    UIView                  *_lineView;
}

@end
@implementation MBForumDetailCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreatUI];
    }
    return self;
}

- (void)onCreatUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //*******************用户评论*****************
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = TINK_BG_COLOR;
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
        _nickNameLb.font = [UIFont systemFontOfSize:28*PERCEN];
    }
    _nickNameLb.numberOfLines = 0;
    [_bgView addSubview:_nickNameLb];
    
    //*******************回复*****************
    _zhanKaiBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _zhanKaiBt.backgroundColor = [UIColor clearColor];
    _zhanKaiBt.hidden = YES;
    [_zhanKaiBt setTitle:@"查看详情>>" forState:UIControlStateNormal];
    [_zhanKaiBt setTitleColor:UIColorFromHex(0x2782d7) forState:UIControlStateNormal];
    _zhanKaiBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
        _nickNameLb.font = [UIFont systemFontOfSize:28*PERCEN];
    }
    _zhanKaiBt.hidden = YES;
    [_zhanKaiBt addTarget:self action:@selector(onSelectForPinLun) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_zhanKaiBt];
    
    _huiFuBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _huiFuBt.backgroundColor = [UIColor clearColor];
    _huiFuBt.hidden = YES;
    [_huiFuBt setTitle:@"回复" forState:UIControlStateNormal];
    [_huiFuBt setTitle:@"回复" forState:UIControlStateHighlighted];
    [_huiFuBt setTitleColor:TINK_COLOR forState:UIControlStateNormal];
    [_huiFuBt setTitleColor:TINK_COLOR forState:UIControlStateHighlighted];
    _huiFuBt.titleLabel.font = _nickNameLb.font;
    [_huiFuBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [_huiFuBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateNormal];
    [_huiFuBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateHighlighted];
    [_huiFuBt setImageEdgeInsets:UIEdgeInsetsMake(0, (WIDTH_5S?20:10), 0, 0)];
    [_huiFuBt addTarget:self action:@selector(onSelectForHuiFu) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_huiFuBt];
    
    //昵称
    _huiNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _huiNameLb.backgroundColor = [UIColor clearColor];
    _huiNameLb.textAlignment = NSTextAlignmentLeft;
    _huiNameLb.textColor = UIColorFromHex(0x333333);
    _huiNameLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
        _huiNameLb.font = [UIFont systemFontOfSize:28*PERCEN];
        _huiNameLb.textColor = UIColorFromHex(0xffffff);
    }
    _huiNameLb.hidden = YES;
    _huiNameLb.numberOfLines = 2;
    [_bgView addSubview:_huiNameLb];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.15];
    [self.contentView addSubview:_lineView];
    
}

//调到评论页面
- (void)onSelectForPinLun
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForSeeCommentData:)]) {
        [_delegate onSelectItemForSeeCommentData:_dataDic];
    }
}

//回复
- (void)onSelectForHuiFu
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForGetCommentData:)]) {
        [_delegate onSelectItemForGetCommentData:_dataDic];
    }
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    if (!_dataDic) {
        _dataDic = [[NSDictionary alloc]init];
    }
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)setShowHuiFu:(BOOL)showHuiFu
{
    _showHuiFu = showHuiFu;
    [self setNeedsLayout];
}

- (void)setForumUserID:(NSString *)forumUserID
{
    _forumUserID = forumUserID;
    [self setNeedsLayout];
}
#pragma mark---转码
- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (!_dataDic) return;
    //value
    NSDictionary *dic = [_dataDic objectForKey:@"ReplyInfo"];

    
    NSMutableParagraphStyle *paragraphHui = [[NSMutableParagraphStyle alloc]init];
    paragraphHui.lineSpacing = 6*PERCEN;
    
    if ([dic isKindOfClass:[NSDictionary class]]) {
        if (dic.allKeys.count == 0) {
            //没有回复内容
            NSString *str1 = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];//@"Karna的爸爸";
            NSString *str2=[self replaceUnicode: [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]]];
        //    NSString *str2 = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]];//@"照片拍的不错，给你个赞！";
            NSString *nick = [NSString stringWithFormat:@"%@: %@",str1,str2];
           
            NSMutableAttributedString *nickAttr = [[NSMutableAttributedString alloc]initWithString:nick];
            
            
            [nickAttr addAttribute:NSForegroundColorAttributeName value:TINK_BG_COLOR range:NSMakeRange(0, str1.length)];
            [nickAttr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x333333) range:NSMakeRange(str1.length+1, str2.length+1)];
            [nickAttr addAttribute:NSParagraphStyleAttributeName value:paragraphHui range:NSMakeRange(0, nick.length)];
            _nickNameLb.attributedText = nickAttr;

        } else {
            //有回复内容
            NSString *str0 = @"回复";
            NSString *Str1 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ReplyName"]];//@"Karna";
            NSString *Str2 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"RepliedName"]];//@"Karna的爸爸";
            NSString *str3=[self replaceUnicode:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ReplyCont"]]];
            
          //  NSString *str3 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ReplyCont"]];//@"照片拍的不错，给你个赞！照片拍的不错，给你个赞！照片拍的不错，给你个赞！照片拍的不错，给你个赞！照片拍的不错，给你个赞！";
            NSString *huiName = [NSString stringWithFormat:@"%@ %@: %@ %@",Str1,str0,Str2,str3];
            NSMutableAttributedString *huiAttr = [[NSMutableAttributedString alloc]initWithString:huiName];
            [huiAttr addAttribute:NSForegroundColorAttributeName value:TINK_BG_COLOR range:NSMakeRange(0, Str1.length)];
            [huiAttr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x333333) range:NSMakeRange(Str1.length+1, str0.length)];
            [huiAttr addAttribute:NSForegroundColorAttributeName value:TINK_BG_COLOR range:NSMakeRange(Str1.length+1+str0.length+2, Str2.length)];
            [huiAttr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x333333) range:NSMakeRange(Str1.length+1+str0.length+2+Str2.length+1, str3.length)];
            [huiAttr addAttribute:NSParagraphStyleAttributeName value:paragraphHui range:NSMakeRange(0, huiName.length)];
            _nickNameLb.attributedText = huiAttr;
        }
    }
    
    //frame
     float nickHeight = [_nickNameLb.attributedText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-2*25*PERCEN, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height;
    if (ISIPAD) {
        nickHeight = [_nickNameLb.attributedText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-2*460*PERCEN, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height;
    }
    
    nickHeight = (nickHeight / 20. + 1) * 20.;
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, nickHeight+10);

    if (ISIPAD) {
        _bgView.frame = CGRectMake(460*PERCEN, 0, SCREEN_WIDTH-2*460*PERCEN, nickHeight+10);
    }
    _nickNameLb.frame = CGRectMake(25*PERCEN, (ISIPAD?20*PERCEN:10), _bgView.width-2*25*PERCEN, nickHeight);
    
    _lineView.frame = CGRectMake(0, 1, _bgView.width, 1);
    if (ISIPAD) {
        _lineView.frame=CGRectMake(_bgView.left, 1, _bgView.width, 1);
    }

}

- (void)onTurnTWoHText:(NSString *)name withCount:(int)countWord withParaph:(NSMutableParagraphStyle *)style
{
    NSString * nameStr = [name substringWithRange:NSMakeRange(0, countWord)];
    nameStr = [NSString stringWithFormat:@"%@...  查看详情>>",nameStr];
    NSMutableAttributedString *nickNAttr = [[NSMutableAttributedString alloc]initWithString:nameStr];
    [nickNAttr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, nameStr.length)];
    float nickHeight = [nickNAttr boundingRectWithSize:CGSizeMake(_nickNameLb.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height;
    if (nickHeight < 40) {
        [self onTurnTWoHText:name withCount:(countWord+1) withParaph:style];
        return;
    }
    
    NSString *nNick = [NSString stringWithFormat:@"%@...",[name substringWithRange:NSMakeRange(0, countWord-6)]];

    NSString *str1 = @"Karna的爸爸";
    NSMutableAttributedString *nickAttr = [[NSMutableAttributedString alloc]initWithString:nNick];
    [nickAttr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x2782d7) range:NSMakeRange(0, str1.length)];
    [nickAttr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x333333) range:NSMakeRange(str1.length, nNick.length-str1.length)];
    [nickAttr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, nNick.length)];
    _nickNameLb.attributedText = nickAttr;

}

@end
